﻿Public Class FuncList_ToolWindow


    Private _ParentForm As MainForm
    Private WithEvents _MetisCore As Core.MetisCore
    Private WithEvents _FuncList As Core.Functions.MetisFunctionList
    Private WithEvents _NamespaceList As Core.Functions.MetisNamespaceList

    Public Event ErrorOccurred(ByVal Message As String)

    Sub New()
        InitializeComponent()
    End Sub

    Sub Init(ByVal MetisCore As Core.MetisCore, ByVal ParentForm As MainForm)
        _MetisCore = MetisCore
        _ParentForm = ParentForm
        If _MetisCore.Connection.State = Data.ConnectionState.Open Then Reload()
    End Sub

    Private Sub HandleConStateChanged(ByVal sender As Object, ByVal e As System.Data.StateChangeEventArgs) Handles _MetisCore.ConnectionStateChange
        If e.CurrentState = Data.ConnectionState.Closed OrElse e.CurrentState = Data.ConnectionState.Broken Then
            tvFunctions.Nodes.Clear()
            btnFunc_Add.Enabled = False
            btnFunc_Del.Enabled = False
            btnFunc_Exec.Enabled = False
            btnFunc_Open.Enabled = False
            btnNamespace_Add.Enabled = False
            btnNamespace_Del.Enabled = False
            btnNamespaceProperties.Enabled = False
            btnReloadFuncList.Enabled = False
        ElseIf e.CurrentState = Data.ConnectionState.Open AndAlso e.OriginalState = Data.ConnectionState.Closed Then
            _NamespaceList = _MetisCore.Namespaces
            _FuncList = _MetisCore.Functions
            btnFunc_Add.Enabled = True
            btnNamespace_Add.Enabled = True
            btnReloadFuncList.Enabled = True
            Reload()
        End If
    End Sub

    Private Sub HandleNamespaceChildListChange(ByVal sender As Object, ByVal e As System.ComponentModel.ListChangedEventArgs)
        Dim tmpSourceList As Core.Functions.MetisNamespaceList = sender
        Dim tmpTargetNodes As TreeNodeCollection
        Dim IndexOffet As Integer = 0

        Dim tmpTargetNode As TreeNode = FindNode(DirectCast(sender, Core.Functions.MetisNamespaceList).Parent, tvFunctions.Nodes)
        If tmpTargetNode IsNot Nothing Then
            tmpTargetNodes = tmpTargetNode.Nodes
        Else
            tmpTargetNodes = tvFunctions.Nodes
        End If


        Select Case e.ListChangedType
            Case System.ComponentModel.ListChangedType.ItemAdded
                If TypeOf tmpSourceList(e.NewIndex) Is Core.Functions.MetisNamespace Then
                    Dim tmpNewNS As Core.Functions.MetisNamespace = tmpSourceList(e.NewIndex)
                    Dim tmpNode As TreeNode = tmpTargetNodes.Insert(e.NewIndex, tmpNewNS.ID.ToString, tmpNewNS.Name, 0, 0)
                    tmpNode.Tag = tmpNewNS
                    tmpNode.ToolTipText = tmpNewNS.Annotation
                    RemoveHandler tmpNewNS.ChildListChanged, AddressOf HandleNamespaceChildListChange
                    AddHandler tmpNewNS.ChildListChanged, AddressOf HandleNamespaceChildListChange

                    For Each item As Core.Functions.IMetisNamespaceReference In tmpNewNS.Children
                        If TypeOf item Is Core.Functions.MetisNamespace Then ReloadChildren(item, tmpNode.Nodes)
                    Next

                ElseIf TypeOf tmpSourceList(e.NewIndex) Is Core.Functions.MetisFunction Then
                    Dim tmpNewNS As Core.Functions.MetisFunction = tmpSourceList(e.NewIndex)
                    Dim tmpNode As TreeNode = tmpTargetNodes.Insert(e.NewIndex, tmpNewNS.ID.ToString, tmpNewNS.Name, 1, 1)
                    tmpNode.Tag = tmpNewNS
                    tmpNode.ToolTipText = tmpNewNS.Annotation
                    tmpNode.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                    tmpNode.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                    tmpNode.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)

                End If


            Case System.ComponentModel.ListChangedType.ItemChanged
                If e.NewIndex < tmpTargetNodes.Count Then
                    tmpTargetNodes(e.NewIndex).Text = tmpSourceList(e.NewIndex).Name
                    tmpTargetNodes(e.NewIndex).Tag = tmpSourceList(e.NewIndex)
                    tmpTargetNodes(e.NewIndex).ToolTipText = tmpSourceList(e.NewIndex).Annotation
                End If

            Case System.ComponentModel.ListChangedType.ItemDeleted
                tmpTargetNodes.RemoveAt(e.NewIndex)

        End Select
    End Sub

    Private Function FindNode(ByVal Entity As Core.IMetisCommonEntity, ByVal NodeList As TreeNodeCollection) As TreeNode
        Dim tmpNode As TreeNode
        For Each node As TreeNode In NodeList
            If node.Tag IsNot Nothing AndAlso node.Tag.Equals(Entity) Then
                tmpNode = node
            Else
                tmpNode = FindNode(Entity, node.Nodes)
            End If
            If tmpNode IsNot Nothing Then Return tmpNode
        Next
    End Function

    Public Sub Reload()
        Try
            Me.Cursor = Cursors.WaitCursor
            tvFunctions.SuspendLayout()
            tvFunctions.Nodes.Clear()
            RemoveHandler _NamespaceList.ListChanged, AddressOf HandleNamespaceChildListChange
            _NamespaceList.Reload()
            For Each item As Core.Functions.IMetisNamespaceReference In _MetisCore.Namespaces
                If TypeOf item Is Core.Functions.MetisNamespace Then
                    ReloadChildren(item, tvFunctions.Nodes)
                ElseIf TypeOf item Is Core.Functions.MetisFunction Then
                    Dim Node As TreeNode = tvFunctions.Nodes.Add(item.ID.ToString, item.Name, 1, 1)
                    Node.Tag = item
                    Node.ToolTipText = item.Annotation
                    Node.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                    Node.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                    Node.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
                End If
            Next
            AddHandler _NamespaceList.ListChanged, AddressOf HandleNamespaceChildListChange

        Finally
            tvFunctions.ResumeLayout()
            Me.Cursor = Cursors.Default
            btnFunc_Del.Enabled = (tvFunctions.SelectedNode IsNot Nothing AndAlso TypeOf tvFunctions.SelectedNode.Tag Is Core.Functions.MetisFunction)
            btnFunc_Open.Enabled = (tvFunctions.SelectedNode IsNot Nothing AndAlso TypeOf tvFunctions.SelectedNode.Tag Is Core.Functions.MetisFunction)
            btnFunc_Exec.Enabled = (tvFunctions.SelectedNode IsNot Nothing AndAlso TypeOf tvFunctions.SelectedNode.Tag Is Core.Functions.MetisFunction)
            btnNamespace_Del.Enabled = (tvFunctions.SelectedNode IsNot Nothing AndAlso TypeOf tvFunctions.SelectedNode.Tag Is Core.Functions.MetisNamespace)
            btnNamespace_Add.Enabled = (tvFunctions.SelectedNode Is Nothing OrElse TypeOf tvFunctions.SelectedNode.Tag Is Core.Functions.MetisNamespace)
        End Try

    End Sub

    Private Sub ReloadChildren(ByVal SrcNamespace As Core.Functions.MetisNamespace, ByVal DstNodes As TreeNodeCollection)
        Dim RootNode As TreeNode = DstNodes.Add(SrcNamespace.ID.ToString, SrcNamespace.Name, 0, 0)
        RootNode.Tag = SrcNamespace
        RootNode.ToolTipText = SrcNamespace.Annotation
        RemoveHandler SrcNamespace.ChildListChanged, AddressOf HandleNamespaceChildListChange
        AddHandler SrcNamespace.ChildListChanged, AddressOf HandleNamespaceChildListChange



        For Each item As Core.Functions.IMetisNamespaceReference In SrcNamespace.Children
            If TypeOf item Is Core.Functions.MetisNamespace Then
                ReloadChildren(item, RootNode.Nodes)
            ElseIf TypeOf item Is Core.Functions.MetisFunction Then
                Dim Node As TreeNode = RootNode.Nodes.Add(item.ID.ToString, item.Name, 1, 1)
                Node.Tag = item
                Node.ToolTipText = item.Annotation
                Node.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                Node.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                Node.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
            End If
        Next

    End Sub


    Private Sub btnNamespace_Add_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNamespace_Add.Click, miNamespace_Add.Click
        Dim tmpNamespace As Core.Functions.MetisNamespace
        Dim tmpNode As TreeNode = tvFunctions.SelectedNode
        If tmpNode IsNot Nothing Then
            tmpNamespace = New Core.Functions.MetisNamespace(_MetisCore, _MetisCore.GetTemporaryID, "", tmpNode.Tag)
        Else
            tmpNamespace = New Core.Functions.MetisNamespace(_MetisCore, _MetisCore.GetTemporaryID, "")
        End If


        Dim NSForm As New NamespaceForm(tmpNamespace)
        If NSForm.ShowDialog() = Windows.Forms.DialogResult.OK Then
            If tmpNode IsNot Nothing Then
                DirectCast(tmpNode.Tag, Core.Functions.MetisNamespace).Children.Add(tmpNamespace)
                tmpNode.Expand()
            Else
                _MetisCore.Namespaces.Add(tmpNamespace)
            End If
        End If
    End Sub

    Private Sub btnNamespace_Del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNamespace_Del.Click, miNamespace_Del.Click
        If MsgBox(String.Format(My.Resources.Metis_Namespace_ConfirmDrop.ToString, DirectCast(tvFunctions.SelectedNode.Tag, Core.Functions.MetisNamespace).Name), _
                  MsgBoxStyle.YesNo Or MsgBoxStyle.Question) = MsgBoxResult.Yes Then
            DirectCast(tvFunctions.SelectedNode.Tag, Core.Functions.MetisNamespace).Drop()
        End If
    End Sub

    Private Sub btnNamespaceProperties_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnNamespaceProperties.Click, miNamespaceProperties.Click
        Dim NSForm As New NamespaceForm(tvFunctions.SelectedNode.Tag)
        NSForm.ShowDialog()
    End Sub

    Private Sub btnFunc_Open_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFunc_Open.Click, miFunc_Open.Click
        _ParentForm.OpenMetisEntity(tvFunctions.SelectedNode.Tag)
    End Sub

    Private Sub miAddInternalFunction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miAddInternalFunction.Click
        Dim tmpFunc As Core.Functions.MetisInternalFunction
        Dim tmpNode As TreeNode = tvFunctions.SelectedNode
        If tmpNode IsNot Nothing Then
            tmpFunc = New Core.Functions.MetisInternalFunction(_MetisCore, tmpNode.Tag)
        Else
            tmpFunc = New Core.Functions.MetisInternalFunction(_MetisCore)
        End If

        _ParentForm.OpenMetisEntity(tmpFunc)
    End Sub

    Private Sub miAddInputFunction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miAddInputFunction.Click
        Dim tmpFunc As Core.Functions.MetisInputFunction
        Dim tmpNode As TreeNode = tvFunctions.SelectedNode
        If tmpNode IsNot Nothing Then
            tmpFunc = New Core.Functions.MetisInputFunction(_MetisCore, tmpNode.Tag)
        Else
            tmpFunc = New Core.Functions.MetisInputFunction(_MetisCore)
        End If

        _ParentForm.OpenMetisEntity(tmpFunc)
    End Sub

    Private Sub miAddQueryFunction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miAddQueryFunction.Click
        Dim tmpFunc As Core.Functions.MetisViewFunction
        Dim tmpNode As TreeNode = tvFunctions.SelectedNode
        If tmpNode IsNot Nothing Then
            tmpFunc = New Core.Functions.MetisViewFunction(_MetisCore, tmpNode.Tag)
        Else
            tmpFunc = New Core.Functions.MetisViewFunction(_MetisCore)
        End If

        _ParentForm.OpenMetisEntity(tmpFunc)
    End Sub

    Private Sub miAddTableFunction_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles miAddTableFunction.Click
        Dim tmpFunc As Core.Functions.MetisTableFunction
        Dim tmpNode As TreeNode = tvFunctions.SelectedNode
        If tmpNode IsNot Nothing Then
            tmpFunc = New Core.Functions.MetisTableFunction(_MetisCore, tmpNode.Tag)
        Else
            tmpFunc = New Core.Functions.MetisTableFunction(_MetisCore)
        End If

        _ParentForm.OpenMetisEntity(tmpFunc)
    End Sub

    Private Sub btnFunc_Del_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFunc_Del.Click, miFunc_Del.Click

    End Sub

    Private Sub btnFunc_Exec_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnFunc_Exec.Click, miFunc_Exec.Click

    End Sub

    Private Sub btnReloadFuncList_Click(ByVal sender As System.Object, ByVal e As System.EventArgs) Handles btnReloadFuncList.Click, miReloadFuncList.Click
        Reload()
    End Sub

    Private Sub tvFunctions_MouseDown(ByVal sender As System.Object, ByVal e As System.Windows.Forms.MouseEventArgs) Handles tvFunctions.MouseDown
        Dim tmpNode As TreeNode = tvFunctions.GetNodeAt(e.Location)
        tvFunctions.SelectedNode = tmpNode

        btnFunc_Add.Enabled = (tmpNode Is Nothing OrElse TypeOf tmpNode.Tag Is Core.Functions.MetisNamespace)
        btnFunc_Del.Enabled = (tmpNode IsNot Nothing AndAlso TypeOf tmpNode.Tag Is Core.Functions.MetisFunction)
        btnFunc_Exec.Enabled = (tmpNode IsNot Nothing AndAlso TypeOf tmpNode.Tag Is Core.Functions.MetisTableFunction)
        btnFunc_Open.Enabled = (tmpNode IsNot Nothing AndAlso TypeOf tmpNode.Tag Is Core.Functions.MetisFunction)
        btnNamespace_Add.Enabled = (tmpNode Is Nothing OrElse TypeOf tmpNode.Tag Is Core.Functions.MetisNamespace)
        btnNamespace_Del.Enabled = (tmpNode IsNot Nothing AndAlso TypeOf tmpNode.Tag Is Core.Functions.MetisNamespace AndAlso DirectCast(tmpNode.Tag, Core.Functions.MetisNamespace).Children.Count = 0)
        btnNamespaceProperties.Enabled = (tmpNode IsNot Nothing AndAlso TypeOf tmpNode.Tag Is Core.Functions.MetisNamespace)

        If e.Button = Windows.Forms.MouseButtons.Left AndAlso tmpNode IsNot Nothing Then
            If TypeOf tmpNode.Tag Is Core.Functions.MetisNamespace Then
                tvFunctions.DoDragDrop(DirectCast(tmpNode.Tag, Core.Functions.MetisNamespace), DragDropEffects.Move)
            ElseIf TypeOf tmpNode.Tag Is Core.Functions.MetisFunction Then
                tvFunctions.DoDragDrop(DirectCast(tmpNode.Tag, Core.Functions.MetisFunction), DragDropEffects.Move)
            End If
        End If

    End Sub

    Private Sub mnuFunctionsContext_Opening(ByVal sender As System.Object, ByVal e As System.ComponentModel.CancelEventArgs) Handles mnuFunctionsContext.Opening
        miNamespace_Add.Enabled = btnNamespace_Add.Enabled
        miNamespace_Del.Enabled = btnNamespace_Del.Enabled
        miNamespaceProperties.Enabled = btnNamespaceProperties.Enabled
        miFunc_Add.Enabled = btnFunc_Add.Enabled
        miFunc_Del.Enabled = btnFunc_Del.Enabled
        miFunc_Exec.Enabled = btnFunc_Exec.Enabled
        miFunc_Open.Enabled = btnFunc_Open.Enabled
        miReloadFuncList.Enabled = btnReloadFuncList.Enabled
    End Sub

    Private Sub tvFunctions_DragOver(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tvFunctions.DragOver
        Dim TargetNode As TreeNode = tvFunctions.GetNodeAt(tvFunctions.PointToClient(New System.Drawing.Point(e.X, e.Y)))

        If e.Data.GetDataPresent(GetType(Core.Functions.MetisInputFunction)) OrElse _
           e.Data.GetDataPresent(GetType(Core.Functions.MetisInternalFunction)) OrElse _
           e.Data.GetDataPresent(GetType(Core.Functions.MetisTableFunction)) OrElse _
           e.Data.GetDataPresent(GetType(Core.Functions.MetisViewFunction)) Then

            Dim SrcFunc As Core.Functions.MetisFunction
            If e.Data.GetDataPresent(GetType(Core.Functions.MetisInputFunction)) Then SrcFunc = e.Data.GetData(GetType(Core.Functions.MetisInputFunction))
            If e.Data.GetDataPresent(GetType(Core.Functions.MetisInternalFunction)) Then SrcFunc = e.Data.GetData(GetType(Core.Functions.MetisInternalFunction))
            If e.Data.GetDataPresent(GetType(Core.Functions.MetisTableFunction)) Then SrcFunc = e.Data.GetData(GetType(Core.Functions.MetisTableFunction))
            If e.Data.GetDataPresent(GetType(Core.Functions.MetisViewFunction)) Then SrcFunc = e.Data.GetData(GetType(Core.Functions.MetisViewFunction))

            e.Effect = DragDropEffects.Move
            If TargetNode IsNot Nothing AndAlso _
               (TypeOf TargetNode.Tag Is Core.Functions.MetisFunction OrElse SrcFunc.Equals(TargetNode.Tag)) Then e.Effect = DragDropEffects.None

        ElseIf e.Data.GetDataPresent(GetType(Core.Functions.MetisNamespace)) Then
            Dim SrcNS As Core.Functions.MetisNamespace = e.Data.GetData(GetType(Core.Functions.MetisNamespace))
            e.Effect = DragDropEffects.Move

            'check if the target namespace is a descendant of the source namespace to prevent circular dependencies
            If TargetNode IsNot Nothing Then
                Dim tmpNode As TreeNode = TargetNode
                Do
                    If SrcNS.Equals(tmpNode.Tag) Then
                        e.Effect = DragDropEffects.None
                        Exit Sub
                    Else
                        tmpNode = tmpNode.Parent
                    End If
                Loop Until tmpNode Is Nothing
            End If

        End If
    End Sub

    Private Sub tvFunctions_DragDrop(ByVal sender As System.Object, ByVal e As System.Windows.Forms.DragEventArgs) Handles tvFunctions.DragDrop
        Dim TargetNode As TreeNode = tvFunctions.GetNodeAt(tvFunctions.PointToClient(New System.Drawing.Point(e.X, e.Y)))

        If e.Data.GetDataPresent(GetType(Core.Functions.MetisInputFunction)) OrElse _
           e.Data.GetDataPresent(GetType(Core.Functions.MetisInternalFunction)) OrElse _
           e.Data.GetDataPresent(GetType(Core.Functions.MetisTableFunction)) OrElse _
           e.Data.GetDataPresent(GetType(Core.Functions.MetisViewFunction)) Then

            Dim SrcFunc As Core.Functions.MetisFunction
            If e.Data.GetDataPresent(GetType(Core.Functions.MetisInputFunction)) Then SrcFunc = e.Data.GetData(GetType(Core.Functions.MetisInputFunction))
            If e.Data.GetDataPresent(GetType(Core.Functions.MetisInternalFunction)) Then SrcFunc = e.Data.GetData(GetType(Core.Functions.MetisInternalFunction))
            If e.Data.GetDataPresent(GetType(Core.Functions.MetisTableFunction)) Then SrcFunc = e.Data.GetData(GetType(Core.Functions.MetisTableFunction))
            If e.Data.GetDataPresent(GetType(Core.Functions.MetisViewFunction)) Then SrcFunc = e.Data.GetData(GetType(Core.Functions.MetisViewFunction))

            Try
                If SrcFunc.Namespace IsNot Nothing Then
                    SrcFunc.Namespace.Children.Remove(SrcFunc)
                Else
                    _MetisCore.Namespaces.Remove(SrcFunc)
                End If

                If TargetNode IsNot Nothing AndAlso TypeOf TargetNode.Tag Is Core.Functions.MetisNamespace Then
                    DirectCast(TargetNode.Tag, Core.Functions.MetisNamespace).Children.Add(SrcFunc)
                    TargetNode.Expand()
                Else
                    SrcFunc.Namespace = Nothing
                    _MetisCore.Namespaces.Add(SrcFunc)
                End If

            Catch ex As Core.Exceptions.MetisNameException
                RaiseEvent ErrorOccurred(ex.Message)
            End Try


        ElseIf e.Data.GetDataPresent(GetType(Core.Functions.MetisNamespace)) Then
            Dim SrcNS As Core.Functions.MetisNamespace = e.Data.GetData(GetType(Core.Functions.MetisNamespace))

            Try
                If TargetNode IsNot Nothing Then
                    SrcNS.Move(TargetNode.Tag)
                    TargetNode.Expand()
                Else
                    SrcNS.Move(Nothing)
                End If

            Catch ex As Core.Exceptions.MetisNameException
                RaiseEvent ErrorOccurred(ex.Message)
            End Try


            'Try
            '    SrcNS.BeginEdit()

            '    Dim SrcParentChilds As Core.Functions.MetisNamespaceList
            '    If SrcNS.Parent IsNot Nothing Then
            '        SrcParentChilds = SrcNS.Parent.Children
            '    Else
            '        SrcParentChilds = _MetisCore.Namespaces
            '    End If

            '    If TargetNode IsNot Nothing Then
            '        DirectCast(TargetNode.Tag, Core.Functions.MetisNamespace).Children.Add(SrcNS)
            '        TargetNode.Expand()
            '    Else
            '        _MetisCore.Namespaces.Add(SrcNS)
            '    End If
            '    SrcParentChilds.Remove(SrcNS)
            'Catch ex As Core.Exceptions.MetisNameException
            '    RaiseEvent ErrorOccurred(ex.Message)
            'Finally
            '    SrcNS.EndEdit()
            'End Try

        End If
    End Sub

    Private Sub tvFunctions_AfterSelect(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFunctions.AfterSelect
        btnFunc_Add.Enabled = (TypeOf e.Node.Tag Is Core.Functions.MetisNamespace) '(e.Node Is Nothing OrElse TypeOf e.Node.Tag Is Core.Functions.MetisNamespace)
        btnFunc_Del.Enabled = (e.Node IsNot Nothing AndAlso TypeOf e.Node.Tag Is Core.Functions.MetisFunction AndAlso Not TypeOf e.Node.Tag Is Core.Functions.MetisInternalFunction)
        btnFunc_Exec.Enabled = (e.Node IsNot Nothing AndAlso TypeOf e.Node.Tag Is Core.Functions.MetisTableFunction)
        btnFunc_Open.Enabled = (e.Node IsNot Nothing AndAlso TypeOf e.Node.Tag Is Core.Functions.MetisFunction)
        btnNamespace_Add.Enabled = (e.Node Is Nothing OrElse TypeOf e.Node.Tag Is Core.Functions.MetisNamespace)
        btnNamespace_Del.Enabled = (e.Node IsNot Nothing AndAlso TypeOf e.Node.Tag Is Core.Functions.MetisNamespace AndAlso DirectCast(e.Node.Tag, Core.Functions.MetisNamespace).Children.Count = 0)
        btnNamespaceProperties.Enabled = (e.Node IsNot Nothing AndAlso TypeOf e.Node.Tag Is Core.Functions.MetisNamespace)
    End Sub

    Private Sub tvFunctions_NodeMouseDoubleClick(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeNodeMouseClickEventArgs) Handles tvFunctions.NodeMouseDoubleClick
        If TypeOf e.Node.Tag Is Core.Functions.MetisNamespace Then
            e.Node.Expand()
        ElseIf btnFunc_Open.Enabled Then
            _ParentForm.OpenMetisEntity(e.Node.Tag)
        End If
    End Sub

    Private Sub tvFunctions_AfterExpand(ByVal sender As System.Object, ByVal e As System.Windows.Forms.TreeViewEventArgs) Handles tvFunctions.AfterExpand
        If TypeOf e.Node.Tag Is Core.Functions.MetisFunction Then
            Dim tmpFunc As Core.Functions.MetisFunction = e.Node.Tag
            e.Node.Nodes("FIELDS").Nodes.Clear()
            For Each field As Core.Functions.MetisFunctionField In tmpFunc.Fields
                Dim newNode As TreeNode = e.Node.Nodes("FIELDS").Nodes.Add(field.ID.ToString, field.Name, 2, 2)
                newNode.Tag = field
                newNode.ToolTipText = field.Annotation
                If field.PK Then newNode.NodeFont = New Font(tvFunctions.Font, FontStyle.Bold)
            Next
            e.Node.Nodes("USES").Nodes.Clear()
            For Each func As Core.Functions.MetisFunction In tmpFunc.Uses
                Dim newNode As TreeNode = e.Node.Nodes("USES").Nodes.Add(func.ID.ToString, func.FullName, 1, 1)
                newNode.Tag = func
                newNode.ToolTipText = func.Annotation
                newNode.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                newNode.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                newNode.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
            Next
            e.Node.Nodes("USED").Nodes.Clear()
            For Each func As Core.Functions.MetisFunction In tmpFunc.UsedBy
                Dim newNode As TreeNode = e.Node.Nodes("USED").Nodes.Add(func.ID.ToString, func.FullName, 1, 1)
                newNode.Tag = func
                newNode.ToolTipText = func.Annotation
                newNode.Nodes.Add("FIELDS", My.Resources.Metis_FuncList_Fields.ToString, 1, 1)
                newNode.Nodes.Add("USES", My.Resources.Metis_FuncList_Uses.ToString, 1, 1)
                newNode.Nodes.Add("USED", My.Resources.Metis_FuncList_Used.ToString, 1, 1)
            Next
        End If

    End Sub

End Class

